package com.zbkj.service.model.front.vo.fund;

import com.zbkj.dal.base.enums.FundDirectionEnums;
import com.zbkj.dal.base.enums.FundStatusEnums;
import com.zbkj.dal.common.utils.MoneyUtil;
import com.zbkj.dal.entity.user.UserBalanceRecord;
import com.zbkj.service.model.front.vo.user.FundFlowVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Data
@ApiModel
public class MonthCrabRoeVO {

    @ApiModelProperty("当月收入金额")
    private String incomeAmount;

    @ApiModelProperty("当月已到账金额")
    private String receivedAmount;

    @ApiModelProperty("当月冻结金额")
    private String frozenAmount;

    public static MonthCrabRoeVO generate(List<UserBalanceRecord> userBalanceRecords) {
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal receivedAmount = BigDecimal.ZERO;
        BigDecimal frozenAmount = BigDecimal.ZERO;
        for (UserBalanceRecord userBalanceRecord : userBalanceRecords) {
            if (StringUtils.equals(userBalanceRecord.getStatus(), FundStatusEnums.IN_TRANSIT.getCode())) {
                if (Objects.equals(userBalanceRecord.getType(), FundDirectionEnums.INCOME.getCode())) {
                    totalAmount = totalAmount.add(userBalanceRecord.getAmount());
                    frozenAmount = frozenAmount.add(userBalanceRecord.getAmount());
                } else {
                    totalAmount = totalAmount.subtract(userBalanceRecord.getAmount());
                    frozenAmount = frozenAmount.subtract(userBalanceRecord.getAmount());
                }
            } else if (StringUtils.equals(userBalanceRecord.getStatus(), FundStatusEnums.FINISH.getCode())) {
                if (Objects.equals(userBalanceRecord.getType(), FundDirectionEnums.INCOME.getCode())) {
                    totalAmount = totalAmount.add(userBalanceRecord.getAmount());
                    receivedAmount = receivedAmount.add(userBalanceRecord.getAmount());
                } else {
                    totalAmount = totalAmount.subtract(userBalanceRecord.getAmount());
                    receivedAmount = receivedAmount.subtract(userBalanceRecord.getAmount());
                }
            }

        }

        MonthCrabRoeVO monthCrabRoeVO = new MonthCrabRoeVO();
        monthCrabRoeVO.setIncomeAmount(MoneyUtil.prettyFormat(totalAmount));
        monthCrabRoeVO.setReceivedAmount(MoneyUtil.prettyFormat(receivedAmount));
        monthCrabRoeVO.setFrozenAmount(MoneyUtil.prettyFormat(frozenAmount));
        return monthCrabRoeVO;
    }

}
